﻿<html>
<head>
    <title>驰骋工作流</title>
    <link href="../Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../Scripts/bootstrap/js/jquery.min.js"></script>
    <script src="../Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <link href="../Scripts/bootstrap/bootstrap-paginator/css/bootstrapv3.css" rel="stylesheet"
        type="text/css" />
    <link href="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.min.css" rel="stylesheet"
        type="text/css" />
    <script src="../Scripts/bootstrap/bootstrap-paginator/js/bootstrap-paginator.js"
        type="text/javascript"></script>
    <script src="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.min.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.zh-CN.min.js" type="text/javascript"></script>
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <!-- 引用通用的js文件. -->
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script type="text/javascript" src="../Comm/Gener.js"></script>
    <script src="../../DataUser/JSLibData/CCFormRef.js" type="text/javascript"></script>
    <style type="text/css">
        .keyWordInput
        {
            border-top-right-radius: 5px;
            border-top-left-radius: 5px;
            border-bottom-right-radius: 5px;
            border-bottom-left-radius: 5px;
            height: 30px;
        }
        .menuGroup
        {
            float: left;
        }
        .menuGroup a
        {
            margin-left:10px;
        }
        .menuGroupSelected
        {
            color: Blue; /* 文字颜色 */
            font-weight: bold;
            text-decoration: none;
            padding-bottom:5px;
            border-bottom: 3px solid Blue;
        }
        .searchpanellabel
        {
            font-size:13px;
            margin-left:5px;
            margin-right:5px;
        }
        #searchDiv
        {
            padding-top:5px;
            padding-bottom:5px;
        }
    </style>
    <script type="text/javascript" language="javascript">
        //扩展设置信息
        var mapExt = null;
        //页面启动函数.
        $(function () {
            //重置页面内容
            ResetHtml();
            //实体参数.
            var ensName = GetQueryString("EnsName");
            mapExt = new Entity("BP.Sys.MapExt");
            //mapExt.ExtType = "BillModel";
            mapExt.MyPK = "DtlImp_" + ensName + "_BillModel";
            var i = mapExt.RetrieveFromDBSources();
            if (i == 0) {
                alert("没有配置导入数据相关参数。");
                return;
            }

            var searchSQL = mapExt.Tag;
            if (searchSQL == null || searchSQL == "") {
                alert("没有查询SQL，请联系管理员");
                return;
            }

            SearchData();
            //设置表格内容高度
            SetTableSize();
        });

        //设置表格高度
        function SetTableSize() {
            var height = $(window).innerHeight();
            height = height - 100;
            //表格内容高度
            $("#tableContent").height(height);
        }

        //查询数据
        function SearchData() {
            //SQL语句
            var dbSrc = mapExt.Tag;

            //处理SQL
            //替换表达式常用的用户信息
            var webUser = new WebUser();
            dbSrc = dbSrc.replace('@WebUser.No', webUser.No);
            dbSrc = dbSrc.replace('@WebUser.Name', webUser.Name);
            dbSrc = dbSrc.replace("@WebUser.FK_DeptNameOfFull", webUser.FK_DeptNameOfFull);
            dbSrc = dbSrc.replace('@WebUser.FK_DeptName', webUser.FK_DeptName);
            dbSrc = dbSrc.replace('@WebUser.FK_Dept', webUser.FK_Dept);
            
            //处理SQL 替换URL中的参数
            var refPK = this.GetQueryString("RefPK");
            if(refPK =="" || refPK == undefined){
                alert('没有获取到相关联的外键，不能查询数据');
                return;
            }

            $("#jsonDataTable").html("");
            if (refPK.indexOf(',') == -1) {
                dbSrc = dbSrc.replace('@Key', refPK);
                var data = DBAccess.RunDBSrc(dbSrc, mapExt.DBType, mapExt.FK_DBSrc);
                InitTableByData(data);
                return;
            }

            refPK = refPK.split(',');
            var data = [];

            for (var i = 0; i < refPK.length; i++) {
                if (refPK[i] == "")
                    continue;
                dbSrc = dbSrc.replace('@Key', refPK[i]);
                var pkdata = DBAccess.RunDBSrc(dbSrc, mapExt.DBType, mapExt.FK_DBSrc);
               $.each(pkdata,function(i,obj){
                    data.push(obj);
               });
                
            }
            InitTableByData(data);
        }

        //数据返回Table
        function InitTableByData(dataTable) {
            //获取数据列名
            var columns = [];
            var texts = [];
            var heads = [];
            if (mapExt.Tag5.length > 0) {
                texts = mapExt.Tag5.split(',');
            }
            for (var col in dataTable[0]) {
                columns.push(col);
            }

            //以中文个数为表格显示列的个数
            if (texts && texts.length > 0) {
                $.each(texts, function (i, column) {
                    if (column == "")
                        return;
                    var columns = column.split("=");
                    if (columns.length == 2) {
                        var column = {};
                        column.No = columns[0];
                        column.Name = columns[1];
                        heads.push(column);
                    }
                });
            } else {
                $.each(columns, function (i, col) {
                    var column = {};
                    column.No = col;
                    column.Name = col;
                    heads.push(column);
                });
            }

            //表头
            var thread = $('<thead></thead>');
            var threadTr = $('<tr></tr>');
            //只能选择一个单据信息
            var thFirst = $("<th style='width:35px;'></th>");
            var ckBox = $("<input type='checkbox' onclick='CheckAll()' id='CB_CheckAll' >");
            thFirst.append(ckBox);

            threadTr.append(thFirst);
            $.each(heads, function (i, head) {
                var th = $("<th></th> ");
                th.text(head.Name);
                th.data(head);
                threadTr.append(th);
            });
            thread.append(threadTr);
            $("#jsonDataTable").append(thread);

            if (dataTable != null && dataTable.length > 0) {

                //生成表格体
                var tbody = $('<tbody></tbody>');
                $.each(dataTable, function (i, dataObj) {
                    var tbodyTr = $('<tr></tr>');
                    var tdFirst = $("<td></td>");
                    //第一列为主键
                    var refPK = heads[0].No;
                    var no = dataObj[refPK];

                    var ckBox = $("<input type='checkbox' onclick='changeSta(this)'/>");
                    ckBox.attr("id", "CB_" + no);
                    ckBox.data(dataObj);
                    tdFirst.append(ckBox);

                    tbodyTr.append(tdFirst);
                    //字段列
                    $.each(heads, function (i, head) {
                        var td = $("<td></td>");
                        var text = "";

                        if (head.No && head.No != "") {
                            text = dataObj[head.No];
                        }
                        td.text(text);
                        tbodyTr.append(td);
                    });
                    tbody.append(tbodyTr);
                });
                $("#jsonDataTable").append(tbody);
            }
        }

        //重置标签
        function ResetHtml() {
            $("#groupDiv").html("");
            $("#jsonDataTable").html("");
        }
        function SelectTr(obj) {
            $.each($(":checkbox"), function () {
                this.checked = false;
            });
             
        }

        //全选
        function CheckAll() {
            var checked = document.getElementById('CB_CheckAll').checked;
            $.each($(":checkbox"), function () {
                this.checked = checked;
            });
        }

        function changeSta(obj) {
            if (obj.checked == false)
                document.getElementById('CB_CheckAll').checked = false;
        }


        function DalogModelOK(ImpType) {
            //实体参数.
            var EnsName = GetQueryString("EnsName");
            var refPKVal = GetQueryString("RefPKVal");
            var PWorkID = GetQueryString("PWorkID");
            var FK_Node = GetQueryString("FK_Node");
            var FID = GetQueryString("FID");
            var UserNo = GetQueryString("UserNo");
            var paraDatas = [];
            $.each($(":checkbox"), function (i, cb) {
                if (cb.checked) {
                    var data = $(cb).data();
                    if (data && cb.id != "CB_CheckAll") {
                        paraDatas.push(data);
                    }
                }
            });
            if (paraDatas.length == 0) {
                alert("没有选择导入数据，请选择！");
                return;
            }
            //插入明细表数据
            var pks = mapExt.Tag4;

            var handler = new HttpHandler("BP.WF.HttpHandler.WF_CCForm");
            handler.AddPara("EnsName", EnsName);
            handler.AddPara("RefPKVal", refPKVal);
            handler.AddPara("PWorkID", PWorkID);
            handler.AddPara("FK_Node", FK_Node);
            handler.AddPara("FID", FID);
            //清空数据
            if (ImpType == 0) {
                handler.DoMethodReturnString("DtlImpBySQL_Delete");
            }

            //默认导入从表附件方法
            //获取从表附件的属性 是否为自定义
            var frmAth = new Entity("BP.Sys.FrmUI.FrmAttachmentExt");
            frmAth.SetPKVal(EnsName + "_" + FK_Node + "_AthMDtl");
            var count = frmAth.RetrieveFromDBSources();
            if (count == 0) {
                frmAth.SetPKVal(EnsName + "_AthMDtl");
                frmAth.RetrieveFromDBSources();
            }
           

            $.each(paraDatas, function (i, data) {
                //循环导入数据
                handler.Clear();
                handler.AddPara("EnsName", EnsName);
                handler.AddPara("RefPKVal", refPKVal);
                handler.AddPara("PWorkID", PWorkID);
                handler.AddPara("FK_Node", FK_Node);
                handler.AddPara("FID", FID);
                handler.AddPara("PKs", pks);
                handler.AddPara("UserNo", UserNo);
                handler.AddJson(data);
                //返回导入的从表的OID
                var newdata = handler.DoMethodReturnString("DtlImpBySQl_Imp");
                if (newdata.indexOf("err@") != 0 && newdata != "") {
                    //FK_MapData,附件属性，RefPK,FK_Node
                    afterDtlImp(EnsName, frmAth, newdata, FK_Node, data["oldOID"], data["oldFK_MapData"]);
                }
            });

            //执行完毕关闭弹窗
            window.parent.parent.CloseBootstrapDialog("取消");
        }

        //取消，关闭弹出层
        function DalogModelCose() {
            if (window.parent) {
                window.parent.$('#msgModal').modal('hide');
            }
        }
    </script>
</head>
<body>
    <form id="cc">
    <table style="border: 0px; width: 99%;table-layout: fixed;">
        <tr>
            <td style="border: 1px #FF8000 solid;">
                <div id="tableContent" style="overflow:auto;">
                    <div>
                        <table class="table" id='jsonDataTable'></table>
                    </div>
                    <div style="text-align: left;">
                        <ul id="examplePagetions"></ul>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td style="float:right;">
                <div style="margin-top:10px; margin-right:10px;">
                    <input type="button" class="btn" onclick="DalogModelCose()" value="取消" />
                    <input type="button" class="btn btn-danger" onclick="DalogModelOK(0)" value="清空导入" />
                    <input type="button" class="btn btn-danger" onclick="DalogModelOK(1)" value="追加导入" />
                </div>
            </td>
        </tr>
    </table>
    </form>
    <input type="hidden" id="HD_GroupField" value="" />
</body>
</html>
